using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

namespace Fanfou
{
    /// <summary>
    /// HeyRice class
    /// </summary>
    partial class HeyRice
    {
        /// <summary>
        /// Login to fanfou.com
        /// </summary>
        /// <returns>Return true if login succeed, otherwise return false</returns>
        private static bool Login()
        {
            bool isValid = false;
            Console.WriteLine(AppString.SYSTEM_LOGIN_WELCOME);
            Console.WriteLine();

            Console.Write(AppString.SYSTEM_LOGIN_USERNAME);
            userName = Console.ReadLine();
            Console.Write(AppString.SYSTEM_LOGIN_PASSWORD);
            string password = null;

            // Mark password as '*'
            ArrayList pw = new ArrayList();
            ConsoleKeyInfo cki = Console.ReadKey(true);
            while (cki.Key.ToString() != "Enter")
            {
                pw.Add(cki);
                Console.Write("*");
                cki = Console.ReadKey(true);
            }
            Console.WriteLine();
            foreach (object obj in pw)
            {
                cki = ((ConsoleKeyInfo)obj);
                password = password + cki.KeyChar;
            }

            base64UserInfo = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(userName + ":" + password));

            // Validate user identity
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ApiUrls.USER_VALIDATION);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "GET";
            request.Headers.Add("Authorization", "Basic " + base64UserInfo);
            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
                isValid = true;
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }

            return isValid;
        }

        /// <summary>
        /// Send message
        /// </summary>
        private static void SendMessages()
        {
            ShowBasicScreen();
            Console.WriteLine();
            Console.Write(AppString.USER_POST);
            string message = Console.ReadLine();
            string parameters = string.Empty;

            // Set the API source
            if (message.Length <= 140)
            {
                parameters = "status=" + message + "&source=heyrice";
            }
            else
            {
                parameters = "status=" + message.Substring(0, 140) + "&source=heyrice";
            }
            string apiUrl = ApiUrls.MSG_SEND;
            HttpWebResponse result = PostAndRecieve(parameters, apiUrl);

            if (result.StatusCode == HttpStatusCode.OK)
            {
                Console.WriteLine(AppString.USER_OPERATION_SUCCEED);
            }
            else
            {
                Console.WriteLine(AppString.USER_OPERATION_FAIL);
            }

            Console.ReadKey();
            ShowBasicScreen();
            Console.WriteLine(AppString.SYSTEM_MENU);
            Console.Write(AppString.SYSTEM_USER_COMMAND);
        }

        /// <summary>
        /// Check friends updates
        /// </summary>
        private static void CheckFriends()
        {
            ShowBasicScreen();
            Console.WriteLine();
            Console.WriteLine(AppString.USER_TIMELINE);
            Console.WriteLine();

            string apiUrl = ApiUrls.FRIEND_TIMELINE + userName;
            HttpWebResponse response = GetAndReceive(apiUrl);

            Stream stream = response.GetResponseStream();
            XmlDocument xmlResponse = new XmlDocument();
            xmlResponse.Load(stream);
            stream.Close();

            ParsePosts(xmlResponse);
            Console.WriteLine();
            Console.WriteLine(AppString.USER_OPERATION_SUCCEED);

            Console.ReadKey();
            ShowBasicScreen();
            Console.WriteLine(AppString.SYSTEM_MENU);
            Console.Write(AppString.SYSTEM_USER_COMMAND);
        }

        /// <summary>
        /// Check recent updates
        /// </summary>
        private static void FindRencently()
        {
            ShowBasicScreen();
            Console.WriteLine();
            Console.WriteLine(AppString.USER_RECENT_NEWS);
            Console.WriteLine();

            MessageTimeline myMessageTimeline = new MessageTimeline();
            string url = myMessageTimeline.ComposeTimeLineUrl();

            HttpWebResponse response = GetAndReceive(ApiUrls.USER_TIMELINE);

            Stream stream = response.GetResponseStream();
            XmlDocument xmlResponse = new XmlDocument();
            xmlResponse.Load(stream);
            stream.Close();

            ParsePosts(xmlResponse);
            Console.WriteLine();
            Console.WriteLine(AppString.USER_OPERATION_SUCCEED);

            Console.ReadKey();
            ShowBasicScreen();
            Console.WriteLine(AppString.SYSTEM_MENU);
            Console.Write(AppString.SYSTEM_USER_COMMAND);
        }

        /// <summary>
        /// List friends
        /// </summary>
        private static void ListFriends()
        {
            ShowBasicScreen();
            Console.WriteLine();
            Console.WriteLine(AppString.USER_FRIENDS);
            Console.WriteLine();

            HttpWebResponse response = GetAndReceive(ApiUrls.FRIEND_LIST);

            Stream stream = response.GetResponseStream();
            XmlDocument xmlResponse = new XmlDocument();
            xmlResponse.Load(stream);
            stream.Close();

            ParseFriends(xmlResponse);
            Console.WriteLine();
            Console.WriteLine(AppString.USER_OPERATION_SUCCEED);

            Console.ReadKey();
            ShowBasicScreen();
            Console.WriteLine(AppString.SYSTEM_MENU);
            Console.Write(AppString.SYSTEM_USER_COMMAND);
        }
    }
}
